package com.dibang.app.mvp.util;

import android.Manifest;
import android.annotation.SuppressLint;
import android.support.v4.app.FragmentActivity;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.dibang.app.mvp.abs.BeanFactory;
import com.dibang.app.operation.model.impl.MainModelImpl;
import com.dibang.app.view.AppKeyBoardUtil;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.zaaach.citypicker.CityPicker;
import com.zaaach.citypicker.adapter.OnPickListener;
import com.zaaach.citypicker.model.City;
import com.zaaach.citypicker.model.LocateState;
import com.zaaach.citypicker.model.LocatedCity;

import io.reactivex.functions.Consumer;

public class PickerCity {

    private FragmentActivity activity;

    private OnSelectCityListener listener;

    private static class PickerCityProvider{
        private static PickerCity pickerCity = new PickerCity();
    }

    public static PickerCity getInstance(){
        return PickerCityProvider.pickerCity;
    }

    public PickerCity setActivity(FragmentActivity activity) {
        this.activity = activity;
        return this;
    }

    public PickerCity setListener(OnSelectCityListener listener) {
        this.listener = listener;
        return this;
    }

    public void show(){
        onGetLocationPermissions(activity, new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if(aBoolean){
                    CityPicker picker = CityPicker.from(activity);
                    picker.enableAnimation(true);
                    picker.setLocatedCity(null);
                    picker.setHotCities(BeanFactory.getInstance(MainModelImpl.class).generateHotCity());
                    picker.setOnPickListener(new OnPickListener() {
                        IAMapLocationListener mListener = new IAMapLocationListener();

                        @Override
                        public void onPick(int position, City data) {
                            AppKeyBoardUtil.hideInputMethod(activity);
                            LocationService.stop(mListener);
                            activity = null;
                            if(listener != null){
                                listener.onSelectCity(data);
                            }
                        }

                        @Override
                        public void onLocate() {
                            LocationService.get().init(activity);
                            LocationService.start(mListener);
                            mListener.setLocationListener(picker);
                        }

                        @Override
                        public void onCancel() {
                            LocationService.stop(mListener);
                            activity = null;
                        }



                    });

                    picker.show();
                }
            }
        });
    }

    private class IAMapLocationListener implements AMapLocationListener {

        private CityPicker picker;

        public void setLocationListener(CityPicker picker) {
            this.picker = picker;
        }

        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            if (picker != null) {
                picker.locateComplete(new LocatedCity(aMapLocation.getCity(), aMapLocation.getProvince(), aMapLocation.getCityCode()), LocateState.SUCCESS);
                LocationService.get().unregisterListener(this);
            }
        }
    }

    public interface OnSelectCityListener{
        void onSelectCity(City city);
   }


    /**
     * 申请定位权限
     *
     * @param activity
     * @param onNext
     */
    @SuppressLint("CheckResult")
    private void onGetLocationPermissions(FragmentActivity activity, Consumer<Boolean> onNext) {
        new RxPermissions(activity)
                .request(Manifest.permission.ACCESS_FINE_LOCATION)//这里填写所需要的权限多个的话可以逗号隔开
                .subscribe(onNext);
    }
}
